﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Text.Editor.DragDrop;
using System.Diagnostics;
using FunctionPoint.Common;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
using FunctionPoint.Services;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace FunctionPoint.Handlers
{
    public class CodeDragDropHandler : DropHandlerBase
    {
        internal IDropTarget _dropTarget = null;
        private EnvDTE80.DTE2 _dte2 = null;
        private WorkItem currentWorkItem;
        private IFunctionPointManagerService _managementService = null;


        public IFunctionPointManagerService ManagementService
        {
            get
            {
                if (_managementService == null)
                    _managementService = ServiceProvider.GlobalProvider.GetService(typeof(SFunctionPointManagerService)) as IFunctionPointManagerService;

                return _managementService;
            }
        }
        internal EnvDTE80.DTE2 DTE2
        {
            get
            {
                if (_dte2 == null)
                    _dte2 = ServiceProvider.GlobalProvider.GetService(typeof(SDTE)) as EnvDTE80.DTE2;

                return _dte2;
            }
        }
        public CodeDragDropHandler(IWpfTextView wpfTextView, IEditorOperations editorOperations):
            base(wpfTextView, editorOperations)
        {
            Debug.WriteLine("Entering Constructor for CodeDragDropHandler");
        }



        protected override string ExtractText(DragDropInfo dragDropInfo)
        {
            
            string extractedText = string.Empty;
            if (dragDropInfo.Data.GetDataPresent(Common.Constants.DROPHANDLER_DROPFORMAT_TEXT))
                extractedText = dragDropInfo.Data.GetData(Common.Constants.DROPHANDLER_DROPFORMAT_TEXT).ToString();

            Debug.WriteLine("CodeDragDropHandler:ExtractText:\r\nSource:{0}\r\nLocation:{1},{2}\r\n,ExtractedText:{3} ", new object[] { dragDropInfo.Source, dragDropInfo.Location.X.ToString(), dragDropInfo.Location.Y.ToString(), extractedText });

            return extractedText;
        }

        public override bool IsDropEnabled(DragDropInfo dragDropInfo)
        {
            Debug.WriteLine("CodeDragDropHandler:IsDropEnabled:Source:{0}:Location:{1}", dragDropInfo.Source, dragDropInfo.Location.ToString());

            return true;
            //return base.IsDropEnabled(dragDropInfo);
        }

        protected override bool MoveText(Microsoft.VisualStudio.Text.VirtualSnapshotPoint position, IList<Microsoft.VisualStudio.Text.ITrackingSpan> selectionSpans, string data)
        {
            Debug.WriteLine("CodeDragDropHandler:MoveText");
            return base.MoveText(position, selectionSpans, data);
        }

        protected override void SelectText(Microsoft.VisualStudio.Text.SnapshotPoint insertionPoint, int dataLength, DragDropInfo dragDropInfo, bool reverse)
        {
            Debug.WriteLine("CodeDragDropHandler: SelectText");

            base.SelectText(insertionPoint, dataLength, dragDropInfo, reverse);
        }

        protected override void PerformPostEditActions(DragDropInfo dragDropInfo, bool successfulEdit)
        {
            Debug.WriteLine("CodeDragDropHandler: PerformPostEditActions");
            return;
        }

        protected override void PerformPreEditActions(DragDropInfo dragDropInfo)
        {
            Debug.WriteLine("CodeDragDropHandler: PerformPreEditActions");

            return;
        }

        protected override DragDropPointerEffects GetDragDropEffect(DragDropInfo dragDropInfo)
        {
            Debug.WriteLine("CodeDragDropHandler:GetDragDropEffect:VirtualBufferPosition:{0}:Location:{1}\r\n-----", dragDropInfo.VirtualBufferPosition.Position, dragDropInfo.Location.ToString());    
            return base.GetDragDropEffect(dragDropInfo);
        }
        public override DragDropPointerEffects HandleDataDropped(DragDropInfo dragDropInfo)
        {
            Debug.WriteLine("CodeDragDropHandler:HandleDataDropped:VirtualBufferPosition:{0}:Location:{1}\r\n-----", dragDropInfo.VirtualBufferPosition.Position, dragDropInfo.Location.ToString());    
            return base.HandleDataDropped(dragDropInfo);
        }

        public override DragDropPointerEffects HandleDraggingOver(DragDropInfo dragDropInfo)
        {
            Debug.WriteLine("CodeDragDropHandler:HandleDraggingOver:VirtualBufferPosition:{0}:Location:{1}\r\n-----", dragDropInfo.VirtualBufferPosition.Position, dragDropInfo.Location.ToString());    
            return base.HandleDraggingOver(dragDropInfo);
        }

        public override DragDropPointerEffects HandleDragStarted(DragDropInfo dragDropInfo)
        {
            Debug.WriteLine("CodeDragDropHandler:HandleDragStarted:VirtualBufferPosition:{0}:Location:{1}\r\n-----", dragDropInfo.VirtualBufferPosition.Position, dragDropInfo.Location.ToString());    
            return base.HandleDragStarted(dragDropInfo);
        }

        protected override bool InsertText(Microsoft.VisualStudio.Text.VirtualSnapshotPoint position, string data)
        {
            Debug.WriteLine("CodeDragDropHandler:InsertText");
            // return base.InsertText(position, data);
            return true;
        }
    }
}
